const GoogleTranslate = {
    single: (text, srouce='auto', target='zh-CN') => {
        return new Promise((resolve => {
            const url = `https://translate.googleapis.com/translate_a/single?client=gtx&format=html&sl=${srouce}&tl=${target}&dt=t&q=${encodeURIComponent(text)}`
            $.ajax({
                headers: {
                },
                type: 'GET',
                url: url,
                success: (data) => {
                    let retStr = ''
                    if (data && typeof data !== 'string' && data.length > 0) {
                        data = data[0]
                        for (let i = 0; i < data.length; i++) {
                            let cnText = data[i]
                            while (cnText && typeof cnText !== 'string' && cnText.length > 0) {
                                cnText = cnText[0]
                            }
                            retStr += cnText
                        }
                    }

                    resolve(retStr)
                },
                error: (err) => {
                    console.error(err)
                    resolve(null)
                }
            });
        }))
    },
    batch: (textArray, srouce='auto', target='zh-CN') => {
        return new Promise((resolve => {
            $.ajax({
                headers: {
                    Accept: "application/json; charset=utf-8",
                    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"',
                    'sec-ch-ua-mobile': '?0',
                    'sec-ch-ua-platform': '"Windows"',
                    'sec-fetch-dest': 'empty',
                    'sec-fetch-mode': 'cors',
                    'sec-fetch-site': 'cross-site',
                },
                type: 'POST',
                url: 'https://translate.googleapis.com/translate_a/t?anno=3&client=te_lib&format=html&v=1.0&sl=en&tl=zh-CN&tc=1&sr=1&tk=618838.1019298&mode=1',
                data: commetText.join('&'),
                success: (data) => {
                    resolve(data)
                },
                error: (err) => {
                    console.error(err)
                    resolve([...commetText, '====>>>翻译失败，返回原文<<<=====']) // 原文返回
                },
                dataType: 'application/x-www-form-urlencoded'
            });
        }))
    }
}